import BaseList from '../base/BaseList.js';
import TimNet from '../../timnet.js';
import MessListItem from './MessListItem.js';
import MessListPointItem from "./MessListPointItem.js";
import Common from '../../common/common.js';

function MessList () {
    BaseList.call(this);
    // 用于续拉，分页续拉时需传入该字段。
    this.nextReqMessageID;
    this.isCompleted;
    this.conversationData;
    this.element.className = "";
    this.conversationID;
 }
let prototype = Object.create(BaseList.prototype);
prototype.constructor = MessList;
MessList.prototype  = prototype;


//
MessList.prototype.loadMessageList = function(isFrist) {
    if(this.isCompleted){return;}
    TimNet.getMessageList(this.conversationID, this.nextReqMessageID, (result) => {
        this.nextReqMessageID = result.data.nextReqMessageID;
        this.isCompleted = result.data.isCompleted;
        const scrollHeight = this.element.parentElement.scrollHeight;
        this.setBeforeItems(result.data.messageList.map(message=>{
            if (message.isRevoked){
               return new MessListPointItem("撤回一条消息","info").appendUserName(message.from);
            } else{
                return new MessListItem(message,this);
            }
        }))
        if(isFrist){
            this.scrollToBottom();
        }else{
            this.element.parentElement.scrollTop =this.element.parentElement.scrollHeight-scrollHeight;
        }
     });
}

//滚动到底部
MessList.prototype.scrollToBottom = function(){
    this.element.parentElement.scrollTop = this.element.parentElement.scrollHeight;
}

MessList.prototype.setBeforeItems = function(items){
    for (let i = items.length-1;i>=0;i--){
        this.items.push(items[i])
        this.element.insertBefore(items[i].element,this.element.childNodes[0]);
    }
}

MessList.prototype.pushMess = function(mess){
    if(this.conversationID == mess.conversationID){
        this.pushItem(new MessListItem(mess,this));
        this.scrollToBottom();
    }
 }

//会话
MessList.prototype.resetByConversation = function(conversation) {
    this.clearItems();
    this.conversationID = conversation.conversationID;
    this.conversationData = conversation;
    this.isCompleted = false;
    this.nextReqMessageID = null;
    this.loadMessageList(true);
}
//获取发送的ID
MessList.prototype.getMessToID = function(){
    if(this.conversationData.type == TIM.TYPES.CONV_C2C){
        return this.conversationData.userProfile.userID;
    }else if(this.conversationData.type == TIM.TYPES.CONV_GROUP){
        return this.conversationData.groupProfile.groupID;
    }else{
        return false;
    }
}

MessList.prototype.sendSuccess = function(data){
    this.pushItem(new MessListItem(data,this));
    this.scrollToBottom();
}

MessList.prototype.sendError = function(data){
    this.pushItem(new MessListPointItem(data,"error"));
    this.scrollToBottom();
}
MessList.prototype.sendInfo = function(data){
    this.pushItem(new MessListPointItem(data,"info"));
    this.scrollToBottom();
}

//发送文本信息
MessList.prototype.sendTextMess = function(text){

    const toID = this.getMessToID();
    if(toID){
        TimNet.sendTextMessage(text,this.conversationData.type,toID,(result)=>{
            this.sendSuccess(result.data.message);
        },(data)=>{
            this.sendError(data);
        });
    }else{
        this.sendError("不支持回复");
    }
}

//发送大表情
MessList.prototype.sendFaceMess = function(bigEmojiIndex){
    const toID = this.getMessToID();
    if(toID){
        TimNet.sendFaceMessage(bigEmojiIndex,"",this.conversationData.type,toID,(result)=>{
            this.sendSuccess(result.data.message);
        },(data)=>{
            this.sendError(data);
        });
    }else{
        this.sendError("不支持回复");
    }
}
//获取文件相关数据
MessList.prototype.crestrMessData = function(){
    let data = {
        isProgress:true,
        progress:0,
        from:Common.cache.myProfile.userID,
        flow: "out",
        time:parseInt(new Date().getTime()/1000,10)
     }
     return data;
}
//发送文件消息
MessList.prototype.sendFileMess = function(fileInput){
    const toID = this.getMessToID();
    if(toID){
        const fileUrl = fileInput.value;
        const fileInfo = Common.tool.getFileInfo(fileUrl);

        let data = this.crestrMessData();
        data.type = TIM.TYPES.MSG_FILE;
        data.payload = {
            fileName: fileInfo.name,
            fileUrl: fileUrl
        };
        const item =  new MessListItem(data,this);
        this.pushItem(item);
        this.scrollToBottom();

        TimNet.sendFileMessage(fileInput,this.conversationData.type,toID,(success)=>{
            item.setData(success.data.message)
        },(error)=>{
            this.sendError(error);
        },(progress)=>{
            item.updateProgress(progress);
        })

    }else{
        this.sendError("不支持回复");
    }
}

//发送图片
MessList.prototype.sendImageMess = function(fileInput){
    const toID = this.getMessToID();
    if(toID){
        const fileUrl = URL.createObjectURL(fileInput.files[0]);

        let data = this.crestrMessData();
        data.type = TIM.TYPES.MSG_IMAGE;
        data.payload = {
           imageInfoArray:[
               {  url:   fileUrl,  type:0 },
               {  url:   fileUrl,  type:1 },
               {  url:   fileUrl,  type:2 }
           ]
         };
        const item =  new MessListItem(data,this);
        this.pushItem(item);
        this.scrollToBottom();

        TimNet.sendImageMessage(fileInput,this.conversationData.type,toID,(success)=>{
            item.setData(success.data.message)
        },(error)=>{
            this.sendError(error);
        },(progress)=>{
            item.updateProgress(progress);
        })

    }else{
        this.sendError("不支持回复");
    }
}

//发送图片
MessList.prototype.sendVideoMess = function(fileInput){
    const toID = this.getMessToID();
    if(toID){
        const fileUrl = URL.createObjectURL(fileInput.files[0]);

        let data = this.crestrMessData();
        data.type = TIM.TYPES.MSG_VIDEO;
        data.payload = {
            videoUrl:fileUrl
        };
        const item =  new MessListItem(data,this);
        this.pushItem(item);
        this.scrollToBottom();

        TimNet.sendVideoMessage(fileInput,this.conversationData.type,toID,(success)=>{
            item.setData(success.data.message)
        },(error)=>{
            this.sendError(error);
        },(progress)=>{
            item.updateProgress(progress);
        })

    }else{
        this.sendError("不支持回复");
    }
}


export default MessList;
